import { Role } from '@/types/role';

import apiClient from '../apiClient';

export interface WorkFetchReq {
  keywords?: string;
  page?: number;
  page_size?: number;
  searchText?: string;
  user_id?: string;
}

export interface WorkCreateReq {
  app_type: string;
  template_app_id: string;
  app_name: string;
}

export interface MessageReq {
  notification_id?: string | Array<string>;
  status?: string;
  page?: number;
  pageSize?: number;
  searchText?: string;
  startTime?: string;
  endTime?: string;
  notificationId?: string | Array<string>;
}

export interface AddRoleReq {
  name: string;
  description: string;
  parent_id?: string | undefined;
}

export interface AddAppTagReq {
  name: string;
  sort_order?: number;
}

export enum WorkApi {
  Public = '/openapi/v2/aiapp/list',
  MessageInform = '/openapi/v1/notifications/notifications',
  MessageDetail = '/openapi/v1/notifications/detail',
  UpdateMessageInform = '/openapi/v1/notifications/handle',
  WorkList = '/openapi/v2/apps/list',
  CopyWorkData = '/openapi/v2/apps/create',
  DeleteWorkData = '/openapi/v2/apps/delete',
  DeleteMessageInform = '/openapi/v1/notifications/delete',
  GetReviewCount = '/openapi/v2/proxy/review/count',

  // SignIn = '/auth/signin',
  SignIn = '/openapi/v1/user/loginByPassword',
  // SignIn = '/ruigpt/help/user/account/loginByPassword',
  // AuthCx = '/openapi/v1/user/cx/sso/login',
  AuthCx = '/openapi/v1/user/cx/sso/login_create',
  SignUp = '/auth/signup',
  Logout = '/auth/logout',
  Refresh = '/auth/refresh',
  Permissions = '/openapi/v1/permissions',
  Role = '/openapi/v1/role',
  FetchRoleApi = '/openapi/v1/roles',
  UpdateRoleDebug = '/openapi/v1/debug/switch_role',
  // RolePermissions = '/openapi/v1/role_permissions',
  RolePermissions = '/openapi/v1/role',
  LicenseApi = '/agent/api/v1/license',
  LicenseRedeemApi = '/agent/api/v1/redeem_license_url',
  // 用户管理
  RuigptWorkApi = '/openapi/v1/user',

  // 应用标签
  AppTagListApi = '/openapi/v2/apps/tags/list',
  addAppTagApi = '/openapi/v2/apps/tags/create',
  delAppTagApi = '/openapi/v2/apps/tags/delete',
}
// 公共应用列表
const publicApi = (params: WorkFetchReq) => apiClient.get({ url: WorkApi.Public, params });
// 消息通知
const messageInformApi = () => apiClient.get({ url: WorkApi.MessageInform });
// 消息通知详情
const messageDetailApi = (data: MessageReq) => apiClient.post({ url: WorkApi.MessageDetail, data });
// 删除消息通知
const deleteMessageInformApi = (data: MessageReq) =>
  apiClient.post({ url: WorkApi.DeleteMessageInform, data });
// 更改消息通知
const updateMessageInformApi = (data: MessageReq) =>
  apiClient.post({ url: WorkApi.UpdateMessageInform, data });
// 工作台应用列表
const workListApi = (params: WorkFetchReq, signal?: AbortSignal) =>
  apiClient.get({ url: WorkApi.WorkList, params, signal });
// 复制工作台应用
const copyWorkDataApi = (data: WorkCreateReq) =>
  apiClient.post({ url: WorkApi.CopyWorkData, data });
// 删除工作台应用
const deleteWorkDataApi = (id: string) =>
  apiClient.delete({ url: `${WorkApi.DeleteWorkData}/${id}` });
// 获取工作台的图标数量
const getReviewCountApi = (params: MessageReq) =>
  apiClient.get({ url: WorkApi.GetReviewCount, params });

const logout = (params: WorkFetchReq) => apiClient.get({ url: WorkApi.Logout, params });
const permissions = () => apiClient.get({ url: WorkApi.Permissions });
// const updatePermissions = () => apiClient.put({ url: WorkApi.Permissions });
const addRole = (data: AddRoleReq) => apiClient.post<Role>({ url: WorkApi.Role, data });
const deleteRole = (id: string) => apiClient.delete({ url: `${WorkApi.Role}/${id}` });

// 获取角色列表，包括conditions和permissions
const getRole = () => apiClient.get<Role[]>({ url: WorkApi.Role });

// const getRoles = () => apiClient.get({ url: WorkApi.UpdateRole});
const getRoles = () => apiClient.get({ url: `${WorkApi.FetchRoleApi}?applicationId=999` });

// const getRolePermissions = () => apiClient.get({ url: WorkApi.RolePermissions });
const getRolePermissions = (role_id = '') => {
  let url: string = WorkApi.RolePermissions;
  if (role_id) {
    url += `?role_id=${role_id}`;
  }
  return apiClient.get({ url });
};
// License 导入和信息获取
const fetchLicense = () => apiClient.get({ url: WorkApi.LicenseApi });
const fetchLicenseUrl = () => apiClient.get({ url: WorkApi.LicenseRedeemApi });
// 用户管理
const fetchUser = (data: WorkFetchReq) =>
  apiClient.get({ url: WorkApi.RuigptWorkApi, params: data });
const deleteUser = (id: string) => apiClient.delete({ url: `${WorkApi.RuigptWorkApi}/${id}` });
const updateRoleType = (id: string) =>
  apiClient.post({ url: `${WorkApi.RolePermissions}/${id}` + `/subadmin` });

// 工作台应用标签模块
// 获取应用标签
const getAppTagList = () => apiClient.get({ url: WorkApi.AppTagListApi });
// 新增应用标签
const addAppTag = (data: AddAppTagReq) => apiClient.post({ url: WorkApi.addAppTagApi, data });
// 删除应用标签
const delAppTag = (id: string) => apiClient.delete({ url: `${WorkApi.delAppTagApi}/${id}` });

export default {
  publicApi,
  messageInformApi,
  updateMessageInformApi,
  workListApi,
  copyWorkDataApi,
  deleteWorkDataApi,
  messageDetailApi,
  deleteMessageInformApi,
  getReviewCountApi,

  logout,
  permissions,
  deleteRole,
  addRole,
  getRole,
  getRoles,
  getRolePermissions,

  fetchUser,
  deleteUser,

  fetchLicense,
  fetchLicenseUrl,
  updateRoleType,

  getAppTagList,
  addAppTag,
  delAppTag,
};
